package com.xingyun.mvp.ui.home.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.xingyun.R;
import com.xingyun.mvp.ui.home.adapter.HomeViewPagerAdapter;
import com.xingyun.mvp.ui.home.fragment.LiveFragment;
import com.xingyun.mvp.ui.home.fragment.MainPagerFragment;
import com.xingyun.mvp.ui.home.fragment.MyFragment;
import com.xingyun.mvp.ui.home.fragment.VideoFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Cyw on 2017/9/29.
 */

public class HomeActivity extends AppCompatActivity {

    @BindView(R.id.vp_homePager)
    ViewPager vpHomePager;
    @BindView(R.id.tab_mainPager)
    LinearLayout tabMainPager;
    @BindView(R.id.tab_live)
    LinearLayout tabLive;
    @BindView(R.id.tab_video)
    LinearLayout tabVideo;
    @BindView(R.id.tab_mine)
    LinearLayout tabMine;

    private List<Fragment> fragmentList = new ArrayList<>();
    private HomeViewPagerAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        initData();
    }


    private void initData() {
        setSelected(tabMainPager, true);

        fragmentList.add(MainPagerFragment.newInstance());
        fragmentList.add(LiveFragment.newInstance());
        fragmentList.add(VideoFragment.newInstance());
        fragmentList.add(MyFragment.newInstance());

        if (adapter == null) {
            adapter = new HomeViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        }

        vpHomePager.setAdapter(adapter);
        vpHomePager.setCurrentItem(0);
        vpHomePager.setOffscreenPageLimit(fragmentList.size() - 1);

        vpHomePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.vp_homePager, R.id.tab_mainPager, R.id.tab_live, R.id.tab_video, R.id.tab_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tab_mainPager:
                setCurrentTab(0);
                break;
            case R.id.tab_live:
                setCurrentTab(1);
                break;
            case R.id.tab_video:
                setCurrentTab(2);
                break;
            case R.id.tab_mine:
                setCurrentTab(3);
                break;
        }
    }

    private void setCurrentTab(int currentIndex) {
        vpHomePager.setCurrentItem(currentIndex);
        switch (currentIndex) {
            case 0:
                setSelected(tabMainPager, true);
                setSelected(tabLive, false);
                setSelected(tabVideo, false);
                setSelected(tabMine, false);
                break;
            case 1:
                setSelected(tabMainPager, false);
                setSelected(tabLive, true);
                setSelected(tabVideo, false);
                setSelected(tabMine, false);
                break;
            case 2:
                setSelected(tabMainPager, false);
                setSelected(tabLive, false);
                setSelected(tabVideo, true);
                setSelected(tabMine, false);
                break;
            case 3:
                setSelected(tabMainPager, false);
                setSelected(tabLive, false);
                setSelected(tabVideo, false);
                setSelected(tabMine, true);
                break;
        }
    }

    /**
     * 设置tab颜色
     *
     * @param viewGroup
     * @param isSelect
     */
    private void setSelected(ViewGroup viewGroup, boolean isSelect) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            viewGroup.getChildAt(i).setSelected(isSelect);
        }
    }
}
